//
//  WCNPastPaientsVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/2.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNPastPaientsVC.h"
#import "WCNWoundRecordVC.h"
//
#import "WCNPastPatientTCell.h"
#import "MJRefresh.h"
#import "NoRecordsView.h"
//
#import "WCDefaultManager.h"
#import "WCCoreDataStore.h"
#import "WCNetworkAccessManager.h"
//
#import "Macros.h"
#import "Constants.h"
#import "WCNPastPatientModel.h"
#import "WCNPiccPastPatientCell.h"
#import "HistoryPatient.h"
#import "WCNNewPICCPreserveVC.h"
#import "WCNPatientAddVC.h"
#import "WCNPiccRecordListVC.h"


static NSString *cellId = @"WCNPastPatientTCell";


@interface WCNPastPaientsVC ()

@property (strong, nonatomic) NSMutableArray *woundPatients;
@property (strong, nonatomic) NSMutableArray *piccPatients;

@property (nonatomic,  strong) UISegmentedControl *segment;
@property (nonatomic,  assign) NSInteger  currentIndex;

@property (nonatomic,  assign) NSInteger patientType;

@property (nonatomic,  assign) BOOL firstClick;

@end

@implementation WCNPastPaientsVC

- (instancetype)init
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        self.title = @"过往患者";
        self.firstClick = YES;
        self.currentIndex = 0;
        _woundPatients = [NSMutableArray array];
        _piccPatients  =[NSMutableArray array];
//        [_patients addObjectsFromArray:[[WCCoreDataStore shareStore] getHistoryPatients]];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kAppBg;
    
    self.navigationItem.titleView =  self.segment;
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNPastPatientTCell" bundle:nil] forCellReuseIdentifier:@"WCNPastPatientTCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNPiccPastPatientCell" bundle:nil] forCellReuseIdentifier:@"WCNPiccPastPatientCell"];
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf getPastPatients:YES];
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf getPastPatients:NO];
    }];
    [self.tableView gzwLoading:^{
       
            [weakSelf getPastPatients:YES];
        
    }];
    [self.tableView.header beginRefreshing];
}
- (UISegmentedControl *)segment{
    if (!_segment) {
        _segment = [[UISegmentedControl alloc] initWithItems:@[@"伤口患者",@"PICC患者"]];
        _segment.frame = CGRectMake(0, 0, 120, 30);
       _segment.tintColor = [UIColor orangeColor];
        _segment.selectedSegmentIndex = 0;
        [_segment addTarget:self action:@selector(changePaient:) forControlEvents:UIControlEventValueChanged];
    }
    return _segment;
                
}
#pragma mark -切换
- (void)changePaient:(UISegmentedControl *)seg{
    if (seg.selectedSegmentIndex == self.currentIndex) {
        return;
    }
    self.currentIndex  =seg.selectedSegmentIndex;
  
    [self.tableView.header beginRefreshing];
   
   
}
- (void)dealloc{

}
- (void)getPastPatients:(BOOL)isRecover{

   
    static NSInteger page =1;
    if (isRecover) {
        page =1;
        
    }else{
        
        page++;
    }
    
//nurseId: 护士ID
//category:TAB标签(1:伤口，2：PICC)选填:
//patientType: 患者类型(业务扩展,默认不传同时查询1和2,1:住院患者,2:门诊患者,3:其他)
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    
    params[@"nurseId"] =@([[WCDefaultManager shareMg] nurseId]);
    if (self.currentIndex ==0) {
         params[@"category"] = @(1);
    }else{
        params[@"category"] = @(2);
    
    }
    params[@"page"] = @(page);
    params[@"rows"] = @(10);
    
    defineWeakSelf;
    weakSelf.tableView.loading = YES;
    [[WCNetworkAccessManager sharedMg] POST:kGetPastPatientList parameters:params success:^(NSDictionary *responseObject) {
        
        if (isRecover && weakSelf.currentIndex==0) {
            [weakSelf.woundPatients removeAllObjects];
        }else if(isRecover && weakSelf.currentIndex==1){
             [weakSelf.piccPatients removeAllObjects];
        }
        if (weakSelf.currentIndex==0 ) {
            NSArray *array =responseObject[@"data"][@"result"];
            if (array.count>0) {
                  [self.woundPatients addObjectsFromArray:[[WCCoreDataStore shareStore] createHistoryPatients:array save:YES]];
            }
          
        }else{
            for (NSDictionary *dict in responseObject[@"data"][@"result"]) {
                if ([dict isKindOfClass:[NSDictionary class]]) {
                    
                        WCNPastPatientModel *model =[WCNPastPatientModel
                                                     new];
                        [model setKeyValues:dict];
                        [weakSelf.piccPatients addObject:model];
                   
                    
                }
            }
        
        }
       
        

        [weakSelf.tableView reloadData];
        
    } failure:nil netError:nil always:^{
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
    
    
}

//- (void)dealPatients:(NSArray *)data
//{
//    NSMutableArray *array1 = [NSMutableArray array];
//    NSMutableArray *array2 = [NSMutableArray array];
//    
//    for (Patient *patient in data) {
//        NSString *type = patient.nursingType;
//        if ([@"RESIDENT_PATIENT" isEqualToString:type]) {
//            [array1 addObject:patient];
//        } else if ([@"FAMILY_PATIENT" isEqualToString:type]) {
//            [array2 addObject:patient];
//        }
//    }
//    
//    [self.woundPatients removeAllObjects];
//    //组合统一病区的护理对象
//    if (array1.count > 0) {
//        int i = 0;
//        Patient *patient = array1[i]; [array1 removeObjectAtIndex:i];
//        //住院 分为两个科 用
//        NSMutableArray *array3 = [NSMutableArray arrayWithObjects:patient, nil];
//        while (true) {
//            if (i == array1.count && array1.count != 0) {
//                [self.woundPatients addObject:array3];
//                i = 0;
//                patient = array1[i]; [array1 removeObjectAtIndex:i];
//                array3 = [NSMutableArray arrayWithObjects:patient, nil];
//            }
//            
//            if (array1.count == 0) {
//                [self.woundPatients addObject:array3];
//                break;
//            }
//            
//            Patient *patientNext = array1[i];
//            if ([patient.hosWardName isEqualToString:patientNext.hosWardName]) {
//                [array3 addObject:patientNext];
//                [array1 removeObjectAtIndex:i];
//            } else {
//                i++;
//            }
//        }
//    }
//    
//    if (array2.count > 0) {
//        [self.woundPatients addObject:array2];
//    }
//}


#pragma mark - <UITableViewDelegate, UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.currentIndex == 0) {
        return self.woundPatients.count;
    }
    return self.piccPatients.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.currentIndex == 0) {
         WCNPastPatientTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WCNPastPatientTCell" forIndexPath:indexPath];
        cell.model = self.woundPatients[indexPath.row];
        return cell;
        
    }else{
        WCNPiccPastPatientCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCNPiccPastPatientCell" forIndexPath:indexPath];
        cell.model = self.piccPatients[indexPath.row];
        return cell;
    
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    weakSelf_define;
    if (self.currentIndex==0) {
         Patient *patient = self.woundPatients[indexPath.row];

        
        WCNWoundRecordVC *vc =  [[WCNWoundRecordVC alloc] initWithTitle:patient mode:WCNCareRecordVCModeEdit];
//        WCNPatientAddVC *vc =[WCNPatientAddVC new];
        [self.navigationController pushViewController:vc animated:YES];

    }else{
        WCNPastPatientModel *mode = self.piccPatients[indexPath.row];
        WCNPiccRecordListVC *vc =[WCNPiccRecordListVC new];
        vc.isAutoRefresh = ^(){
            [weakSelf getPastPatients:YES];
        };
        vc.patientName =mode.name;
        vc.piccId = [mode.id integerValue];
//        vc.vcType = WCNewPICCPreserveVCTypeModify;
        [self.navigationController pushViewController:vc animated:YES];
    
    }
    

}

@end
